package com.mdd.front.service.goods.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.goods.OrderGoods;
import com.mdd.common.mapper.goods.OrderGoodsMapper;
import com.mdd.common.utils.TimeUtil;
import com.mdd.front.service.goods.IOrderGoodsService;
import com.mdd.front.validate.PageParam;
import com.mdd.front.validate.goods.OrderGoodsParam;
import com.mdd.front.vo.goods.OrderGoodsDetailVo;
import com.mdd.front.vo.goods.OrderGoodsListVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 订单服务实现类
 */
@Service
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoods> implements IOrderGoodsService {

    @Resource
    OrderGoodsMapper orderGoodsMapper;

    /**
     * 订单服务列表
     *
     * @param pageParam 分页参数
     * @param params    搜索参数
     * @return PageResult<OrderGoodsListVo>
     * @author slien
     */
    @Override
    public PageResult<OrderGoodsListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        QueryWrapper<OrderGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        orderGoodsMapper.setSearch(queryWrapper, params, new String[]{
                "=:orderId@order_id:int",
                "=:goodsId@goods_id:int",
                "like:goodsName@goods_name:str",
                "like:unitName@unit_name:str",
                "=:goodsNum@goods_num:int",
                "=:goodsPrice@goods_price:str",
                "=:totalPrice@total_price:str",
                "=:totalPayPrice@total_pay_price:str",
                "=:isComment@is_comment:int",
                "=:goodsSnap@goods_snap:str",
        });

        IPage<OrderGoods> iPage = orderGoodsMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<OrderGoodsListVo> list = new LinkedList<>();
        for (OrderGoods item : iPage.getRecords()) {
            OrderGoodsListVo vo = new OrderGoodsListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 订单服务详情
     *
     * @param id 主键参数
     * @return OrderGoods
     * @author slien
     */
    @Override
    public OrderGoodsDetailVo detail(Integer id) {
        OrderGoods model = orderGoodsMapper.selectOne(
                new QueryWrapper<OrderGoods>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        OrderGoodsDetailVo vo = new OrderGoodsDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 订单服务新增
     *
     * @param orderGoodsParam 参数
     * @author slien
     */
    @Override
    public void add(OrderGoodsParam orderGoodsParam) {
        OrderGoods model = new OrderGoods();
        model.setOrderId(orderGoodsParam.getOrderId());
        model.setGoodsId(orderGoodsParam.getGoodsId());
        model.setGoodsName(orderGoodsParam.getGoodsName());
        model.setUnitName(orderGoodsParam.getUnitName());
        model.setGoodsNum(orderGoodsParam.getGoodsNum());
        model.setGoodsPrice(orderGoodsParam.getGoodsPrice());
        model.setTotalPrice(orderGoodsParam.getTotalPrice());
        model.setTotalPayPrice(orderGoodsParam.getTotalPayPrice());
        model.setIsComment(orderGoodsParam.getIsComment());
        model.setGoodsSnap(orderGoodsParam.getGoodsSnap());
        orderGoodsMapper.insert(model);
    }

    /**
     * 订单服务编辑
     *
     * @param orderGoodsParam 参数
     * @author slien
     */
    @Override
    public void edit(OrderGoodsParam orderGoodsParam) {
        OrderGoods model = orderGoodsMapper.selectOne(
                new QueryWrapper<OrderGoods>()
                        .eq("id", orderGoodsParam.getId())
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(orderGoodsParam.getId());
        model.setOrderId(orderGoodsParam.getOrderId());
        model.setGoodsId(orderGoodsParam.getGoodsId());
        model.setGoodsName(orderGoodsParam.getGoodsName());
        model.setUnitName(orderGoodsParam.getUnitName());
        model.setGoodsNum(orderGoodsParam.getGoodsNum());
        model.setGoodsPrice(orderGoodsParam.getGoodsPrice());
        model.setTotalPrice(orderGoodsParam.getTotalPrice());
        model.setTotalPayPrice(orderGoodsParam.getTotalPayPrice());
        model.setIsComment(orderGoodsParam.getIsComment());
        model.setGoodsSnap(orderGoodsParam.getGoodsSnap());
        orderGoodsMapper.updateById(model);
    }

    /**
     * 订单服务删除
     *
     * @param id 主键ID
     * @author slien
     */
    @Override
    public void del(Integer id) {
        OrderGoods model = orderGoodsMapper.selectOne(
                new QueryWrapper<OrderGoods>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        orderGoodsMapper.delete(new QueryWrapper<OrderGoods>().eq("id", id));
    }

    @Override
    public void insert(OrderGoods orderGoods) {
        super.save(orderGoods);
    }

    @Override
    public List<OrderGoodsDetailVo> listByOrderIds(List<Long> orderIds) {
        if (CollectionUtils.isEmpty(orderIds)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<OrderGoods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(OrderGoods::getOrderId, orderIds);
        List<OrderGoods> list = super.list(lambdaQueryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return Lists.newArrayList();
        }
        List<OrderGoodsDetailVo> targetList = Lists.newArrayList();
        for (OrderGoods orderGoods : list) {
            OrderGoodsDetailVo detailVo = new OrderGoodsDetailVo();
            BeanUtils.copyProperties(orderGoods, detailVo);
            targetList.add(detailVo);
        }
        return targetList;
    }

    @Override
    public OrderGoods getByOrderId(Long id) {
        LambdaQueryWrapper<OrderGoods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderGoods::getOrderId, id);
        lambdaQueryWrapper.last("limit 1");
        return super.getOne(lambdaQueryWrapper);
    }

    @Override
    public List<OrderGoods> listByGoodsIds(List<Long> goodsIds) {
        if (CollectionUtils.isEmpty(goodsIds)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<OrderGoods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(OrderGoods::getGoodsId, goodsIds);
        return super.list(lambdaQueryWrapper);
    }

    @Override
    public Boolean updateEntityById(OrderGoods orderGoods) {
        return super.updateById(orderGoods);
    }

    @Override
    public Integer getIsCommentOrderGoods() {
        LambdaQueryWrapper<OrderGoods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderGoods::getIsComment, 1);
        lambdaQueryWrapper.eq(OrderGoods::getDeleteTime,null);
        return orderGoodsMapper.selectCount(lambdaQueryWrapper);
    }

}
